package com.itheima.reggie.service.impl;


import com.itheima.reggie.dao.AddressBookDao;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired(required = false)
    private AddressBookDao addressBookDao;
    @Override
    public List<AddressBook> findAll(Long userId) {
        List<AddressBook> addressBookList=  addressBookDao.findAll(userId);
        log.info(userId+"");
        return addressBookList;
    }

    @Override
    public AddressBook findById(Long id) {
        AddressBook addressBook= addressBookDao.findById(id);
        return addressBook;
    }

    /**
     * 修改默认地址
     * @param
     */
    @Override
    public void update(AddressBook addressBook) {
        //1. 把该用户地址全部修改为非默认
        addressBookDao.removeDefaultAddress(addressBook.getUserId());
        //2 重新设置当前地址为默认地址
        addressBookDao.update(addressBook);
    }

    @Override
    public void addUser(AddressBook addressBook) {
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setIsDefault(0);
        addressBookDao.save(addressBook);
    }

    @Override
    public void updateAll(AddressBook addressBook) {
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBookDao.updateAll(addressBook);
    }
    @Override
    public AddressBook getDefaultAddress(Long currentId) {
        return addressBookDao.getDefaultAddress(currentId);
    }
}
